package pay

import java.text.SimpleDateFormat
import java.util.{Calendar, Date}

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import com.trjcn.wxpay.model.request.{PayRequest, WXPayCloseRequest}
import com.trjcn.wxpay.utils.BuildDataUtil
import dto.req.{CloseOrderReqDTO, PayOrderReqDTO}
import dto.resp.{CloseOrderRespDTO, PayOrderRespDTO}
import enums.{PayChannelEnum, PayEnum, ThirdSystemErrorEnum}
import exception.CommonException
import factory.ActionFactory
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.InitializingBean
import org.springframework.stereotype.Service

@Service
class WXPayAction extends BasePayAction with InitializingBean{

  override def getPayChannel: Integer = PayChannelEnum.WX_PAY.getId

  override def buildOrder[T <: PayOrderReqDTO, R >: PayOrderRespDTO](req: T): R = {
    val payRequest = new PayRequest
    val payOrderNo = ""
    payRequest.setOrderNO(payOrderNo)
    payRequest.setAmount(req.getPayAmount.longValue())
    payRequest.setGoodsName(req.getGoodsName)
    payRequest.setNotifyUrl(wxPayProperties.getNotifyURL)
    //val params = new JSONObject()
    //params.put("biz_code", req.getBizCode)
    //payRequest.setAttach(params.toJSONString)
    val calendar = Calendar.getInstance
    calendar.add(Calendar.MINUTE, 10)
    payRequest.setTimeExpire(new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime))
    //payRequest.setAttach("appPay1111");
    //payRequest.setSpbillCreateIp("192.168.1.1");
    log.info("wxpay request params : {}", JSON.toJSONString(payRequest, SerializerFeature.PrettyFormat))
    val payResponse = wxPayService.appPrePay(payRequest)
    log.info("wxpay response result : {}", JSON.toJSONString(payResponse, SerializerFeature.PrettyFormat))
    if (!(payResponse.getReturnCode == wxPayProperties.WX_PAY_SUCCESS)) throw new CommonException(payResponse.getReturnCode, payResponse.getReturnMsg)
    /*if(StringUtils.isBlank(payResponse.getPrepayId())){
          throw new CommonException(payResponse.getResultCode(), payResponse.getReturnMsg());
        }*/
    val resp = new PayOrderRespDTO
    BeanUtils.copyProperties(req, resp)
    resp.setPayOrderNo(payOrderNo)
    resp.setBizCode(req.getBizCode)
    resp.setThirdOrderNo(payResponse.getPrepayId)
    resp.setOrderBody(BuildDataUtil.buildMap(payResponse, wxPayProperties.getMchKey))
    resp.setCreateTime(new Date)
    resp.setUpdateTime(new Date)
    resp.setExpiredTime(calendar.getTime)
    resp
  }

  override def closeOrder[T <: CloseOrderReqDTO, R >: CloseOrderRespDTO](req: T): R = {
    val closeRequest = new WXPayCloseRequest
    closeRequest.setOutTradeNo(req.getPayOrderNo)
    log.info("wxpay close order request params : {}", JSON.toJSONString(closeRequest, SerializerFeature.PrettyFormat))
    val closeResponse = wxPayService.closeOrder(closeRequest)
    log.info("wxpay close order response result : {}", JSON.toJSONString(closeResponse, SerializerFeature.PrettyFormat))
    val resp:CloseOrderRespDTO = new CloseOrderRespDTO
    BeanUtils.copyProperties(req, resp)
    if (!(closeResponse.getResultCode == wxPayProperties.WX_PAY_SUCCESS)) {
      resp.setOrderStatusCode(ThirdSystemErrorEnum.getErrorEnum(closeResponse.getErrCode).getCode)
      resp.setOrderStatusMessage(ThirdSystemErrorEnum.getErrorEnum(closeResponse.getErrCode).getCode)
    }
    else {
      resp.setOrderStatusCode(PayEnum.PayTradeStatusEnum.getByThirdCode(closeResponse.getResultCode.toUpperCase, req.getPayChannel).getCode)
      resp.setOrderStatusMessage(PayEnum.PayTradeStatusEnum.getByThirdCode(closeResponse.getResultCode.toUpperCase, req.getPayChannel).getCode)
    }
    resp
  }

  override def afterPropertiesSet(): Unit = ActionFactory.setPayAction(getPayChannel, this)
}
